#!/bin/bash

# To backup in zip format:
#FORMAT=zip  

# Ta backup em tar gzipped format:
FORMAT=tgz

# Where your zimbra lives
ZHOME=/opt/zimbra

# Path to backup base dir
ZBACKUP=/backup/mailbox

# Where the configuration files for zimbra lives
ZCONFD=$ZHOME/conf

# Use this DATE to have /backup/mailbox/{Mon,Tue,Wed...} directory of backups
#DATE=DATE=`date +"%a"`

# Use this DATE to have (eg.) /backup/mailbox/30112010 
DATE=`date +"%d%m%y"`

# Where to store the files
ZDUMPDIR=$ZBACKUP/$DATE

# Path to zmmailbox
ZMBOX=/opt/zimbra/bin/zmmailbox

# If you dont pass any argument, then I'll backup just what happened from yesterday. If you do, I'll perform a full backup of the account.
if [ $# -eq 0 ]; then
 qd=`date --date='1 days ago' +"%m/%d/%Y"`
 query="&query=after:$qd"
fi

# Check if the backup dir exists, create if not.
if [ ! -d $ZDUMPDIR ]; then
	mkdir -p $ZDUMPDIR
fi

# Querying for the accounts then backing up 
for mbox in `zmprov -l gaa`
do
	$ZMBOX -z -m $mbox getRestURL "//?fmt=${FORMAT}$query" > $ZDUMPDIR/$mbox.${FORMAT}
done


